package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.entity.Attachment;
import com.yqg.education.entity.MonthPlan;
import com.yqg.education.entity.Stage;
import com.yqg.education.mapper.MonthPlanMapper;
import com.yqg.education.service.IAttachmentService;
import com.yqg.education.service.IMonthPlanService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * MonthPlanController 前端控制器
 *
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/month-plan")
public class MonthPlanController {

    @Resource
    private IMonthPlanService monthPlanService;
    @Resource
    private MonthPlanMapper monthPlanMapper;

    @Resource
    private IAttachmentService attachmentService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody MonthPlan monthPlan) {
        boolean b = monthPlanService.saveOrUpdate(monthPlan);
        List<Attachment> attachmentList = new ArrayList<>();
        monthPlan.getAttachment().forEach(att -> {
            if (att.getId() == null) {
                Attachment attachment = new Attachment();
                attachment.setCorrelationId(monthPlan.getId());
                attachment.setCorrelationTable("month_plan");
                attachment.setFileName(att.getFileName());
                attachment.setFilePath(att.getFilePath());
                attachmentList.add(attachment);
            }
        });
        if (!attachmentList.isEmpty()) {
            attachmentService.saveOrUpdateBatch(attachmentList);
        }
        return Result.success(b);
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        MonthPlan monthPlan = monthPlanService.getById(id);
        monthPlan.setIsDelete(1);
        return Result.success(monthPlanService.saveOrUpdate(monthPlan));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<MonthPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<MonthPlan> list = monthPlanService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(monthPlanService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String  search,
                           @RequestParam(required = false) String  stageNum,
                           @RequestParam(required = false) Long  stageId) {
        pageNum = pageNum * pageSize - pageSize;
        List<MonthPlan> monthPlanList = monthPlanMapper.pageList(pageNum, pageSize, search,stageNum,stageId);
        Long count = monthPlanMapper.pageListCount(search,stageNum,stageId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", monthPlanList);
        map.put("total", count);
        return Result.success(map);
    }

}

